import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../views/Login'
// import Home from '../views/Home/'
// import Welcome from '../views/Welcome'
// import User from '../views/User'
// import Rights from '../views/rights/Rights'
// import Roles from '../views/rights/Roles'
// import Categories from '../views/goods/GoodsCate'
// import GoodsParams from '../views/goods/GoodsParams'
// import GoodsList from '../views/goods/GoodsList'
// import GoodsAdd from '../views/goods/GoodsAdd'
// import Order from '../views/Order'
// import Reports from '../views/Reports'
// 登录页
const Login = () => import(/* webpackChunkName: "login" */ '../views/Login')
// 首页
const Home = () => import(/* webpackChunkName: "HomeWelcome" */ '../views/Home/')
const Welcome = () => import(/* webpackChunkName: "HomeWelcome" */ '../views/Welcome')
// 用户列表
const User = () => import(/* webpackChunkName: "user" */ '../views/User')
// 权限管理
const Rights = () => import(/* webpackChunkName: "Rights" */ '../views/rights/Rights')
const Roles = () => import(/* webpackChunkName: "Rights" */ '../views/rights/Roles')
// 商品管理
const Categories = () => import(/* webpackChunkName: "goods" */ '../views/goods/GoodsCate')
const GoodsParams = () => import(/* webpackChunkName: "goods" */ '../views/goods/GoodsParams')
const GoodsList = () => import(/* webpackChunkName: "goods" */ '../views/goods/GoodsList')
const GoodsAdd = () => import(/* webpackChunkName: "goods" */ '../views/goods/GoodsAdd')
// 订单管理
const Order = () => import(/* webpackChunkName: "order" */ '../views/Order')
// 数据管理
const Reports = () => import(/* webpackChunkName: "reports" */ '../views/Reports')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: User
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/categories',
        component: Categories
      },
      {
        path: '/params',
        component: GoodsParams
      },
      {
        path: '/goods',
        component: GoodsList
      },
      {
        path: '/goods/add',
        component: GoodsAdd
      },
      {
        path: '/orders',
        component: Order
      },
      {
        path: '/reports',
        component: Reports
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 这个路由钩子在所有的路由跳转进入之前触发
router.beforeEach((to, form, next) => {
  // console.log(to)
  // console.log(form)
  const token = sessionStorage.getItem('token')
  if (token) {
    next()
  } else {
    if (to.path === '/login') return next()
    return next('/login')
  }
})

export default router
